package com.cjh.wechat.core.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cjh.wechat.commom.pojo.Friend;
import com.cjh.wechat.commom.pojo.Notify;
import com.cjh.wechat.commom.type.ApplyStatus;
import com.cjh.wechat.commom.type.ReadStatus;
import com.cjh.wechat.commom.util.BasicUtil;
import com.cjh.wechat.core.dao.NotifyDao;
import com.cjh.wechat.core.service.FriendService;
import com.cjh.wechat.core.service.NotifyService;

@Service
public class NotifyServiceImpl implements NotifyService
{
    @Resource
    private NotifyDao     notifyDao;
    @Resource
    private FriendService friendService;
    
    @Override
    public void insertSelective(Notify notify)
    {
        notifyDao.insertSelective(notify);
    }
    
    @Override
    public int getUnreadMsgNum(String uid)
    {
        return notifyDao.getUnreadMsgNum(uid);
    }
    
    @Override
    public boolean isExist(String uid, String fromUid, Integer type)
    {
        int num = notifyDao.isExist(uid, fromUid, type);
        return num != 0 ? true : false;
    }
    
    @Override
    public List<Notify> findByUid(String uid, int pageNum, int pageSize)
    {
        int startNum = (pageNum - 1) * pageSize;
        int endNum = (pageNum - 1) * pageSize + pageSize - 1;
        return notifyDao.findByUid(uid, startNum, endNum);
    }
    
    @Override
    public void updateByUidSelective(Notify notify)
    {
        notifyDao.updateByUidSelective(notify);
    }
    
    @Override
    public int findByUidSum(String uid)
    {
        return notifyDao.findByUidSum(uid);
    }
    
    @Override
    public void apply(Notify notify) throws Exception
    {
        notify.setDateTime(BasicUtil.getDateTime());
        notify.setIsRead(ReadStatus.UNREAD.value);
        notify.setContent("申请添加您为好友");
        notify.setType(ApplyStatus.INIT.value);
        notifyDao.insertSelective(notify);
    }
    
    @Override
    @Transactional
    public void agreeApply(Integer no, Integer tno) throws Exception
    {
        Notify notify = notifyDao.selectByPrimaryKey(no);
        notify.setType(ApplyStatus.AGREE.value);
        Notify notify2 = new Notify();
        notify2.setContent(notify.getUid() + "同意了您的好友申请");
        notify2.setDateTime(BasicUtil.getDateTime());
        notify2.setFromUid(notify.getUid());
        notify2.setIsRead(ReadStatus.UNREAD.value);
        notify2.setUid(notify.getFromUid());
        Friend friend = new Friend();
        friend.setUid(notify.getUid());
        friend.setFid(notify.getFromUid());
        friend.setTno(tno);
        Friend friend2 = new Friend();
        friend2.setUid(notify.getFromUid());
        friend2.setFid(notify.getUid());
        friend2.setTno(Integer.parseInt(notify.getFromGroup()));
        notifyDao.updateByPrimaryKeySelective(notify);
        notifyDao.insertSelective(notify2);
        friendService.insert(friend);
        friendService.insert(friend2);
    }
    
    @Override
    @Transactional
    public void refuseApply(Integer no) throws Exception
    {
        Notify notify = notifyDao.selectByPrimaryKey(no);
        notify.setType(ApplyStatus.REFUSE.value);
        notifyDao.updateByPrimaryKeySelective(notify);
        Notify notify2 = new Notify();
        notify2.setContent(notify.getFromUid() + "拒绝了您的好友申请");
        notify2.setDateTime(BasicUtil.getDateTime());
        notify2.setFromUid(notify.getFromUid());
        notify2.setIsRead(ReadStatus.UNREAD.value);
        notify2.setUid(notify.getFromUid());
        notifyDao.insertSelective(notify2);
    }
}
